function myPromise(executor) {
    var self = this;
    self.status = 'pending';
    self.data = undefined;
    self.onResolvedCallback = [];
    self.onRejectedCallback = [];

    function resolve(value) {
        self.status = 'resolved';
        self.data = value;
        // 这里难道不是永远就这一个callback吗
        self.onResolvedCallback.forEach(cb => cb(value))
    }

    function reject(value) {
        self.status = 'rejected';
        self.data = value;
        self.onRejectedCallback.forEach(cb => cb(value))
    }
    try {
        executor(resolve, reject);
    } catch (e) {
        reject(e)
    };
}
myPromise.prototype.then = function(onResolved, onRejected) {
    var self = this;
    var promise2;
    onResolved = typeof onResolved === 'function' ? onResolved : function(v) {}
    onRejected = typeof onRejected === 'function' ? onRejected : function(r) {}

    if(self.status === 'resolved') {
    	return promise2 = new myPromise((res, reject) => {
    		try{
    			// 这一步是进行then里里面的操作
    			var x = onResolved(self.data)
    			x = x || self.data;
    			// console.log('x:', x)
    			// 底下这两步是为下次then作准备
    			if(x instanceof myPromise) {
    				x.then(res, reject)
    			}
    			else res(x);
    		} catch(e) {
    			reject(e)
    		}
    	})
    }

    if(self.status === 'rejected') {
    	return promise2 = new myPromise((res, reject) => {
    		try{
    			var x = onRejected(self.data);
    			x = x || self.data;
    			if(x instanceof myPromise) {
    				x.then(res, reject)
    			}
    			else res(x)
    		} catch(e) {
    			reject(e)
    		}
    	})
    }

    if(self.status === 'pending') {
    	return promise2 = new myPromise((res, reject) => {
    		self.onResolvedCallback.push(val => {
    			try{
    				var x = onResolved(self.data)
    				x = x || self.data;
    				if(x instanceof myPromise) {
    					x.then(res, reject)
    				}
    				else res(x);
    			} catch(e) {
    				reject(e)
    			}
    		})
    	})
    }
};
!function() {
    return new myPromise((res, reject) => {
        // setTimeout(() => {
        // 	console.log('timeout')
        // 	res('look')
        // }, 1000);
        res('lll')
    })
}().then(res => {
    // console.log('res1', res)
   	//  return new myPromise((res, reject) => {
    //     setTimeout(() => {
    //     	console.log('timeout2')
    //     	res('look2')
    //     }, 1000);
    // })
}, reject => {
    console.log('err')
}).then(res => {
	console.log(res)
})